function [W1, W2] = BackpropBatchXOR(W1, W2, X, D)
    alpha = 0.9;
    
    N = size(X, 1);  
    
    dW1sum = zeros(size(W1));  
    dW2sum = zeros(size(W2));  
    
    for k = 1:N
        x = X(k, :)';  
        d = D(k);      
        
        v1 = W1*x;
        y1 = Sigmoid(v1);
        v = W2*y1;
        y = Sigmoid(v);
        
        e = d - y;
        delta = y.*(1-y).*e;
        
        e1 = W2'*delta;
        delta1 = y1.*(1-y1).*e1;
        
        dW1sum = dW1sum + alpha * delta1 * x';
        dW2sum = dW2sum + alpha * delta * y1';
    end
    dWavg1 = dW1sum / N;
    dWavg2 = dW2sum / N;
    W1 = W1 + dWavg1; 
    W2 = W2 + dWavg2;
end

function y = Sigmoid(v)
    y = 1 ./ (1 + exp(-v));
end